package main

import (
	"encoding/json"
	"fmt"
	"log"
	"net/http"
	"strconv"
)

func main() {
	// http://127.0.0.1/add?a=1&b=2
	http.HandleFunc("/add", func(w http.ResponseWriter, req *http.Request) {
		// 1.获取参数
		_ = req.ParseForm()
		fmt.Println("path: ", req.URL.Path)
		// 2.反序列化
		a, _ := strconv.Atoi(req.Form["a"][0])
		b, _ := strconv.Atoi(req.Form["b"][0])
		w.Header().Set("Content-Type", "application/json")
		jData, _ := json.Marshal(map[string]int{
			"data": a + b,
		})
		_, _ = w.Write(jData)
	})

	log.Print("server started at localhost:8080")
	_ = http.ListenAndServe(":8080", nil)
}
